package com.wl.shop.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.wl.shop.dao.RoleMenuDao;
import com.wl.shop.entity.RoleMenu;
import com.wl.shop.service.RoleMenuService;

@Service("roleMenuService")
@Transactional(readOnly = true)
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuDao, RoleMenu> implements  RoleMenuService	{
	
	@Override
	public void saveRoleMenu(String roleId, String menuIds){
		if (StringUtils.isNotBlank(menuIds)) {
			List<RoleMenu> list = Lists.newArrayList();
			String[] menuIdList = menuIds.split(",");
			for (String menuId : menuIdList) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(roleId);
                roleMenu.setMenuId(menuId);
                list.add(roleMenu);
            }
			Map<String, Object> map = Maps.newHashMap();
			map.put("role_id", roleId);
			deleteByMap(map);
			insertBatch(list);
		}
	}
	
	
	@Override
	public List<String> getMenuIdsByRoleId(String roleId) {
		EntityWrapper<RoleMenu> wrapper = new EntityWrapper<RoleMenu>();
		wrapper.eq("role_id", roleId);
		List<RoleMenu> roleMenus = baseMapper.selectList(wrapper);
		List<String> menuIds = Lists.newArrayList();
		for (RoleMenu roleMenu : roleMenus) {
			menuIds.add(roleMenu.getMenuId());
		}
        return menuIds;
    }
}
